<?php
	include_once('header.php');
	if ($_SERVER['REQUEST_METHOD']=='POST') {
		// 如果提交删除按钮，删除相应帖子
		if (isset($_POST['del_submit'])) {
			$article_del_id = $_POST['article_id_hidden'];
			$query_del_article = "delete from `articles` where `id`=".$article_del_id;
			$query_del_article_category = "delete from `articles_category` where `articles_id`=".$article_del_id;
			$query_del_comment = "delete from `comment` where `articles_id`=".$article_del_id;
			$query_del_toupiao = "delete from `toupiao_ip` where `article_id`=".$article_del_id;
			$mysql_connection->query($query_del_article);
			$mysql_connection->query($query_del_article_category);
			$mysql_connection->query($query_del_comment);
			$mysql_connection->query($query_del_toupiao);
		}
		// 如果提交删除按钮，删除相应帖子
		// start 如果是修改按钮，转到修改页面
		if (isset($_POST['edit_submit'])) {
			$article_edit_id = $_POST['article_id_hidden'];
		?> 
		<meta http-equiv="Refresh" content="0;URL=new_post.php?edit_id=<?php echo $article_edit_id;?>" />
		<?php
		}
		// end 如果是修改该按钮，转到修改页面
	}

	// start 设置或者获取每页显示帖子数
	if (isset($_GET['per_count'])) {
		$count_per_page = floor($_GET['per_count']);
	} else {
		$count_per_page = 5;
	}
	// end 设置或者获取每页显示帖子数

	// start 取得当前页数
	if (isset($_GET['page'])) {
		$num_page_article = floor($_GET['page']);
	} else {
		$num_page_article = 1;
	}
	// end 取得当前页数

	$count_article = count_table($mysql_connection, 'articles');	// 取得帖子总数量
	$count_page_article = ceil($count_article/$count_per_page);	// 取得页数
	$offset_article = ($num_page_article-1)*$count_per_page;	// 取得 limit 的 offset 值
	$query_edit_article = "select * from `articles` order by `post_time` desc limit ".$offset_article.",".$count_per_page;
	$result_edit_article = $mysql_connection->query($query_edit_article);
	$count_edit_article = $result_edit_article->num_rows;
?>
<div id="content_b">

<!-- start 上面关于显示页面页数的信息 -->
<div id="header_edit_comment">
	<?php
		// start 显示当前显示的评论范围和总评论数量
		if (($num_page_article)*$count_per_page<=$count_article) {
			echo '第 '.(($num_page_article-1)*$count_per_page).'- '.($num_page_article*$count_per_page).' 项, 共 '.$count_article.' 项';
		} else {
			echo '第 '.(($num_page_article-1)*$count_per_page).'- '.($count_article).' 项, 共 '.$count_article.' 项';
		}
		// end 显示当前显示的评论范围和总评论数量
		// start 显示页面导航
	?>
	<span class="space2"></span>
	<!-- start 显示上一页 -->
	<?php
		if ($num_page_article<=1) {
			echo '上一页';
		} else {
		?>
		<a href="edit_del_article.php?page=<?php echo $num_page_article-1;?>&per_count=<?php echo $count_per_page;?>">上一页</a>
		<?php
		}
	?>
	<!-- end 显示上一页 -->
	<span class="space2"></span>第<?php
		if ($num_page_article!=1) {
		?>
		<span class="num_page_edit_comment"><a href="edit_del_article.php?page=1&per_count=<?php echo $count_per_page;?>">1</a></span>
		<?php
		} else {
			echo '<span class="num_page_edit_comment">1</span>';
		}

		if (($count_page_article-2)>3) {
			if ($num_page_article<=4 or $num_page_article>=($count_page_article-3)) {
				if ($num_page_article<=($count_page_article/2)) {
					for ($n=2; $n<=4; $n++) {
						if ($num_page_article!=$n) {
							echo '<span class="num_page_edit_comment"><a href="edit_del_article.php?page='.$n.'&per_count='.$count_per_page.'">'.($n).'</a></span>';
						} else {
							echo '<span class="num_page_edit_comment">'.$n.'</span>';
						}
					}
				?>
				<span class="num_page_edit_comment">...</span>
				<?php
				} else {
				?>
				<span class="num_page_edit_comment">...</span>
				<?php
					for ($p=$count_page_article-3; $p<=$count_page_article-1; $p++) {
						if ($num_page_article!=$p) {
							echo '<span class="num_page_edit_comment"><a href="edit_del_article.php?page='.$p.'&per_count='.$count_per_page.'">'.$p.'</a></span>';
						} else {
							echo '<span class="num_page_edit_comment">'.$p.'</span>';
						}
					}
				}
			} else {
			?>
			<span class="num_page_edit_comment">...</span>
			<?php
				for ($r=($num_page_article-1); $r<=($num_page_article+1); $r++) {
					if($num_page_article==$r) {
						echo '<span class="num_page_edit_comment">'.$r.'</span>';
					} else {
					?>
					<span class="num_page_edit_comment"><a href="edit_del_article.php?page=<?php echo $r;?>&per_count=<?php echo $count_per_page;?>"><?php echo $r;?></a></span>
					<?php
					}
				}
			?>
			<span class="num_page_edit_comment">...</span>
			<?php
			}
		} else if (0<($count_page_article-2) and ($count_page_article-2)<=3) {
			for ($m=1; $m<=($count_page_article-2); $m++) {
				if ($num_page_article==($m+1)) {
					echo '<span class="num_page_edit_comment">'.($m+1).'</span>';
				} else {
					echo '<span class="num_page_edit_comment"><a href="edit_del_article.php?page='.($m+1).'&per_count='.$count_per_page.'">'.($m+1).'</a></span>';
				}
			}
		}
		
		if ($count_page_article!=1) {
			if ($num_page_article!=$count_page_article) {
			?>
			<span class="num_page_edit_comment"><a href="edit_del_article.php?page=<?php echo $count_page_article;?>&per_count=<?php echo $count_per_page;?>"><?php echo $count_page_article;?></a></span>
			<?php
			} else {
				echo '<span class="num_page_edit_comment">'.$count_page_article.'</span>';
			}
		}
		// end 显示页面导航
	?>页<span class="space2"></span>
	<!-- start 显示下一页 -->
	<?php
		if ($num_page_article>=$count_page_article) {
			echo '下一页';
		} else {
		?>
		<a href="edit_del_article.php?page=<?php echo $num_page_article+1;?>&per_count=<?php echo $count_per_page;?>">下一页</a>
		<?php
		}
	?>
	<!-- end 显示下一页 -->

	<!-- start 设定每页显示的评论数量 -->
	<span class="space2"></span>每页显示评论数：
	<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
		<?php
			for ($u=1; $u<=625; $u=$u*5) {
				if ($count_per_page==$u) {
				?>
				<option selected="selected" value="edit_del_article.php?page=1&per_count=<?php echo $u;?>"><?php echo $u;?></option>
				<?php
				} else {
				?>
				<option value="edit_del_article.php?page=1&per_count=<?php echo $u;?>"><?php echo $u;?></option>
				<?php
				}
			}
		?>
	</select>
	<!-- end 设定每页显示的评论数量 -->
</div>
<!-- end 上面关于显示页面页数的信息 -->


<table border="1" width="100%">
	<tr><td>帖子ID</td><td>帖子标题</td><td>发表时间</td><td>修改</td><td>删除</td></tr>
	<?php
		for ($i=0; $i<$count_edit_article; $i++) {
			$each_edit_article = $result_edit_article->fetch_assoc();
		?> 
		<tr><form action="<?php echo $_SERVER['PHP_SELF']; ?>?message_box=帖子已经删除成功" method="post">
			<td>
				<?php echo $each_edit_article['id'];?>
				<input type="hidden" value="<?php echo $each_edit_article['id'];?>" name="article_id_hidden" id="article_id_hidden" />
			</td>
			<td><?php echo $each_edit_article['title'];?></td>
			<td><?php echo $each_edit_article['post_time'];?></td>
			<td><input type="submit" value="修改" name="edit_submit" id="edit_submit" /></td>
			<td><input type="submit" value="删除" name="del_submit" id="del_submit" onclick="return confirm('确定要删除此篇文章吗?');" /></td>
		</form></tr>
		<?php
		}
	?>
</table></div>
<?php include_once('footer.php'); ?> 
